Skip to contentMethod: lambda$inferDataPropertyValues$1(Assertion, OWLLiteral)
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: * <p>
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.model.*;
18: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
19: import cz.cvut.kbss.ontodriver.owlapi.exception.ReasonerNotAvailableException;
20: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
21: import org.semanticweb.owlapi.model.*;
22: import org.semanticweb.owlapi.reasoner.OWLReasoner;
23:
24: import java.util.*;
25: import java.util.stream.Collectors;
26: import java.util.stream.Stream;
27:
28: public class InferredAxiomLoader implements AxiomLoader {
29:
30: private final OWLReasoner reasoner;
31: private final OWLOntology ontology;
32: private final OWLDataFactory dataFactory;
33:
34: private final OwlapiAdapter adapter;
35: private final AxiomAdapter axiomAdapter;
36:
37: private NamedResource subject;
38:
39: InferredAxiomLoader(OwlapiAdapter adapter, OntologySnapshot snapshot) {
40: this.adapter = adapter;
41: this.reasoner = snapshot.getReasoner();
42: this.ontology = snapshot.getOntology();
43: this.dataFactory = snapshot.getDataFactory();
44: this.axiomAdapter = new AxiomAdapter(snapshot.getDataFactory(), adapter.getLanguage());
45: }
46:
47: @Override
48: public Collection<Axiom<?>> loadAxioms(NamedResource subject, Set<Assertion> assertions) {
49: this.subject = subject;
50: if (assertions.isEmpty()) {
51: return Collections.emptySet();
52: }
53: if (reasoner == null) {
54: throw new ReasonerNotAvailableException();
55: }
56: reasoner.flush();
57: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
58: final Collection<Axiom<?>> axioms = new HashSet<>();
59: for (Assertion a : assertions) {
60: switch (a.getType()) {
61: case CLASS:
62: axioms.addAll(adapter.getTypesHandler().getTypes(subject, null, true));
63: break;
64: case DATA_PROPERTY:
65: axioms.addAll(inferDataPropertyValues(individual, a));
66: break;
67: case OBJECT_PROPERTY:
68: axioms.addAll(inferObjectPropertyValues(individual, a));
69: break;
70: case PROPERTY:
71: // When we don't know, try all
72: axioms.addAll(adapter.getTypesHandler().getTypes(subject, null, true));
73: axioms.addAll(inferDataPropertyValues(individual, a));
74: axioms.addAll(inferObjectPropertyValues(individual, a));
75: break;
76: default:
77: break;
78: }
79: }
80: return axioms;
81: }
82:
83: private Collection<Axiom<?>> inferDataPropertyValues(OWLNamedIndividual individual, Assertion dpAssertion) {
84: final Set<OWLLiteral> literals = reasoner.getDataPropertyValues(individual, dataProperty(dpAssertion));
85: final String lang = dpAssertion.hasLanguage() ? dpAssertion.getLanguage() : adapter.getLanguage();
86: return literals.stream().filter(lit -> OwlapiUtils.doesLanguageMatch(lit, lang))
87: .map(owlLiteral -> new AxiomImpl<>(subject, dpAssertion,
88: new Value<>(OwlapiUtils.owlLiteralToValue(owlLiteral)))).collect(Collectors.toSet());
89: }
90:
91: private OWLDataProperty dataProperty(Assertion dataPropertyAssertion) {
92: return dataFactory.getOWLDataProperty(IRI.create(dataPropertyAssertion.getIdentifier()));
93: }
94:
95: private Collection<Axiom<?>> inferObjectPropertyValues(OWLNamedIndividual individual, Assertion opAssertion) {
96: final Stream<OWLNamedIndividual> individuals =
97: reasoner.getObjectPropertyValues(individual, objectProperty(opAssertion)).entities();
98: return individuals.map(
99: target -> axiomAdapter.createAxiom(subject, opAssertion, NamedResource.create(target.getIRI().toURI())))
100: .collect(
101: Collectors.toList());
102: }
103:
104: private OWLObjectProperty objectProperty(Assertion objectPropertyAssertion) {
105: return dataFactory.getOWLObjectProperty(IRI.create(objectPropertyAssertion.getIdentifier()));
106: }
107:
108: @Override
109: public Collection<Axiom<?>> loadPropertyAxioms(NamedResource subject) {
110: final Collection<Axiom<?>> axioms = new ArrayList<>();
111: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
112: ontology.dataPropertiesInSignature().forEach(dp -> {
113: final Set<OWLLiteral> values = reasoner.getDataPropertyValues(individual, dp);
114: for (OWLLiteral literal : values) {
115: axioms.add(axiomAdapter.createAxiom(subject,
116: Assertion.createDataPropertyAssertion(dp.getIRI().toURI(), true), literal));
117: }
118: });
119: ontology.objectPropertiesInSignature().forEach(op -> {
120: final Assertion opAss = Assertion.createObjectPropertyAssertion(op.getIRI().toURI(), true);
121: reasoner.getObjectPropertyValues(individual, op).entities()
122: .forEach(ind -> axioms
123: .add(axiomAdapter.createAxiom(subject, opAss, NamedResource.create(ind.getIRI().toURI()))));
124: });
125: return axioms;
126: }
127: }